/*
    Copyright 2008, Mark Arvidson
    
    This file is part of EwaScorecard.

    EwaScorecard is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    EwaScorecard is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with EwaScorecard.  If not, see <http://www.gnu.org/licenses/>.
*/
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class LoadMeetDataForm extends Form implements CommandListener {
    private ChoiceGroup filename;
    private Command backCommand = new Command("Cancel", Command.SCREEN, 2);
    private Command okCommand = new Command("OK", Command.SCREEN, 1);
    private Command deleteCommand = new Command("Delete current", Command.SCREEN, 3);
    private EwaScorecard host;
    
    public LoadMeetDataForm(EwaScorecard host) {
        super("Load meet data");
        this.host = host;
        setupForm();
    }
    
    public void setupForm() {
        deleteAll();
        String[] recordStores = RecordStore.listRecordStores();
        addCommand(backCommand);
        if (recordStores == null) {
            append(new StringItem("No stored meets available",null));
        } else {
            filename = new ChoiceGroup("Filename", Choice.POPUP, recordStores == null ? new String[] {} : recordStores, null);
            append(filename);
            addCommand(deleteCommand);
            addCommand(okCommand);
        }
        setCommandListener(this);
    }
    
    public void commandAction(Command c,  Displayable d) {
        if (c == okCommand) {
            try {
                String recordName = filename.getString(filename.getSelectedIndex());
                MeetData data = MeetData.retrieveRecord(recordName);
                host.setMeetData(data);            
            } catch (RecordStoreFullException e) {
                Alert alert = new Alert("Could not load","Out of memory",null, AlertType.ERROR);
                Display.getDisplay(host).setCurrent(alert);
            } catch (Exception ex) {
                Alert alert = new Alert("Could not load",ex.getMessage(),null, AlertType.ERROR);
                Display.getDisplay(host).setCurrent(alert);
            }
        } else if (c == deleteCommand) {
            try {
                String recordName = filename.getString(filename.getSelectedIndex());
                RecordStore.deleteRecordStore(recordName);
            } catch (Exception ex) {
                Alert alert = new Alert("Could not load",ex.getMessage(),null, AlertType.ERROR);
                Display.getDisplay(host).setCurrent(alert);
            }
            setupForm();
        } else if (c == backCommand) {
            host.mainMenu();
        }    
    }
}
